package com.qyhl.module_practice.newhome.code;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.permissions.RxPermissions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerDetailBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.weavey.loading.lib.LoadingLayout;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_QR_CODE)
public class PracticeQRCodeActivity extends BaseActivity implements PracticeQRCodeContract.PracticeQRCodeView {
    @BindView(R2.id.name)
    TextView name;
    @BindView(R2.id.qr_img)
    ImageView qrImg;
    @BindView(R2.id.head_icon)
    RoundedImageView headIcon;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.to_scan)
    ImageButton toScan;
    @BindView(R2.id.title)
    TextView title;

    private String volId;

    private PracticeQRCodePresenter mPresenter;

    private boolean isHome = false;

    private String titleName;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_qr_code;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        volId = getIntent().getStringExtra("volId");
        isHome = getIntent().getBooleanExtra("isHome", true);
        titleName = getIntent().getStringExtra("title");
        mPresenter = new PracticeQRCodePresenter(this);
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
        mPresenter.getDetail(volId);
    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        if (StringUtils.isEmpty(titleName)) {
            title.setText("打卡签到");
        } else {
            title.setText(titleName);
        }
        if (isHome) {
            toScan.setVisibility(View.VISIBLE);
        } else {
            toScan.setVisibility(View.GONE);
        }
    }

    private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getDetail(volId);
            }
        });
    }


    @Override
    public void setDetail(PracticeVolunteerDetailBean bean) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        name.setText(bean.getName());

        Glide.with(this)
                .load(bean.getLogo())
                .apply(new RequestOptions().placeholder(R.drawable.comment_head_default).error(R.drawable.comment_head_default))
                .into(headIcon);

        int px = StringUtils.dip2px(this, 200);

        if (StringUtils.isNotEmpty(bean.getQrcode())) {
            Bitmap image = CodeUtils.createImage(bean.getQrcode().trim(), px, px, null);
            qrImg.setImageBitmap(image);
        }
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(this)) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }


    @OnClick({R2.id.back_btn, R2.id.to_scan})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        } else if (id == R.id.to_scan) {

            RxPermissions permissions = new RxPermissions(this);
            permissions
                    .request(Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if( granted ){
                            RouterManager.router(ARouterPathConstant.PRACTICE_SCAN);
                        }
                    });

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_QR_CODE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_QR_CODE);
    }

}
